<?php namespace App\Foundation\Console;

use Illuminate\Console\GeneratorCommand;

class RepositoryMakeCommand extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:repository';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new repository class';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Repository';

    public function handle()
    {
        $name = $this->getNameInput();

        list($class_name, $interface_name) = $this->getClassName($name);

        $class_name_path = $this->getPath($class_name);
        $interface_name_path = $this->getPath($interface_name);
        if ($this->alreadyExists($class_name)) {
            $this->error($this->type . ' already exists!');

            return false;
        }

        $this->makeDirectory($class_name_path);
        $this->makeDirectory($interface_name_path);

        $stub = $this->files->get($this->getStub());
        $content = $this->replaceNamespace($stub, $class_name)
            ->replaceInterface($stub, $interface_name)
            ->replaceClass($stub, $class_name);
        $this->files->put($class_name_path, $content);

        $stub = $this->files->get($this->getInterfaceStub());
        $content = $this->replaceNamespace($stub, $interface_name)->replaceClass($stub, $interface_name);
        $this->files->put($interface_name_path, $content);

        $this->info('Class created successfully: ' . str_replace(base_path() . '/', '', $class_name_path));
        $this->info('Interface created successfully: ' . str_replace(base_path() . '/', '', $interface_name_path));
    }

    /**
     * Replace the namespace for the given stub.
     *
     * @param  string $stub
     * @param  string $name
     *
     * @return $this
     */
    protected function replaceInterface(&$stub, $interface_name)
    {
        $stub = str_replace(
            ['DummyInterface'],
            [$interface_name],
            $stub
        );

        return $this;
    }


    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__ . '/stubs/repository.stub';
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getInterfaceStub()
    {
        return __DIR__ . '/stubs/repository-interface.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string $rootNamespace
     *
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Repositories';
    }

    protected function getClassName($name)
    {
        $name = str_replace('/', '\\', $name);
        $class_name = $name . 'Repository';
        $interface_name = $name . 'RepositoryInterface';

        $rootNamespace = $this->rootNamespace();
        $class_name = $rootNamespace . 'Repositories\Eloquent\\' . $class_name;
        $interface_name = $rootNamespace . 'Repositories\\' . $interface_name;

        return [$class_name, $interface_name];
    }
}
